package work.chenbo.springboot.groovy;

import groovy.lang.Tuple;
import groovy.lang.Tuple2;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @author ChenBo
 * @className CompletableFutureDemo
 * @date 2020/11/24
 */
public class CompletableFutureDemo {

    @Test
    public void test() throws ExecutionException, InterruptedException, JSONException {
//        runAsync1();
//        runAsync2();
//        supplyAsync1();
        completedFuture();
    }

    /**
     * 1,异步计算,无返回值
     * @author ChenBo
     * @date 2020/11/24
     */
    public void runAsync1(){
        CompletableFuture.runAsync(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName());
            }
        });
    }

    /**
     * 无返回值,指定线程池
     * @author ChenBo
     * @date 2020/11/24
     */
    public void runAsync2(){
        CompletableFuture.runAsync(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName());
            }
        }, Executors.newFixedThreadPool(2));
    }

    /**
     * 有返回值
     * @author ChenBo
     * @date 2020/11/24
     */
    public void supplyAsync1() throws ExecutionException, InterruptedException {
        CompletableFuture<Object> objectCompletableFuture = CompletableFuture.supplyAsync(new Supplier<Object>() {
            @Override
            public Object get() {
                System.out.println(Thread.currentThread().getName()+"执行..");
                return "返回值";
            }
        },Executors.newFixedThreadPool(2));
        objectCompletableFuture.whenComplete(new BiConsumer<Object, Throwable>() {
            @Override
            public void accept(Object o, Throwable throwable) {
                System.out.println("result = "+ o);
                throwable.printStackTrace();
            }
        });
    }

    public void completedFuture() throws JSONException {
        JSONObject jsonObject = new JSONObject();
        JSONObject put = jsonObject.put("name", "dfd");
        Tuple2<Object, String> key = new Tuple2<>(put, "key");
        CompletableFuture<Tuple2<Object, String>> tuple2CompletableFuture = CompletableFuture.completedFuture(key);
        tuple2CompletableFuture.thenApplyAsync(new Function<Tuple2<Object, String>, Object>() {
            @Override
            public Object apply(Tuple2<Object, String> objectStringTuple2) {
                HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
                objectObjectHashMap.put(objectStringTuple2.getSecond(),objectStringTuple2.getFirst());
                System.out.println("上边线程执行...,"+objectObjectHashMap);
                return objectObjectHashMap;
            }
        }).thenApplyAsync(new Function<Object, Object>() {

                    @Override
                    public Object apply(Object o) {
                        System.out.println("下面线程执行...,接受的参数:"+o);
                        return o;
                    }
                });
    }
}
